package cn.smbms.controller;

import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.smbms.pojo.ReactiveUser;
import cn.smbms.service.ReactiveService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/reactive/")
public class ReactiveController {
    @GetMapping("echo")
    public Mono<String> sayHelloWorld() {
        return Mono.just("Echo!");
    }
    
    @Autowired
    private ReactiveService userService;

    @GetMapping("list")
    public Flux<ReactiveUser> list() {
        return this.userService.list();
    }

    @GetMapping("{id}")
    public Mono<ReactiveUser>getById(@PathVariable("id") final String id) {
        return this.userService.getById(id);
    }

    @PostMapping("create")
    public Mono<ReactiveUser> create(@RequestBody final ReactiveUser user) {
        return this.userService.createOrUpdate(user);
    }

    @PutMapping("{id}")
    public Mono<ReactiveUser> update(@PathVariable("id") final String id, @RequestBody final ReactiveUser user) {
        Objects.requireNonNull(user);
        user.setId(id);
        return this.userService.createOrUpdate(user);
    }

    @DeleteMapping("{id}")
    public Mono<ReactiveUser>  delete(@PathVariable("id") final String id) {
        return this.userService.delete(id);
    }    
}
